package com.example.zlw.dem12;


import com.sun.jndi.cosnaming.IiopUrl;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @program: Zlw
 * @description:
 * @author: 诸葛龙辰
 * @create: 2022-11-06 17:07
 **/
/*第二遍*/
public class Test1 {
    public static void main(String[] args) {
        List<Zoo> list = new ArrayList<>();
        list.add(new Zoo("郑州熊熊森林",18,"河南"));
        list.add(new Zoo("郑州佩奇养殖场",20,"河南"));
        list.add(new Zoo("四川黑猪森林",6,"四川"));
        list.add(new Zoo("黑猪佩奇俱乐部",60,"郑州"));
        list.add(new Zoo("野猪佩奇养殖场",3,"东北"));
        list.add(new Zoo("野猪佩奇杀猪场",30,"河北"));
        list.add(new Zoo("黑猪佩奇屠宰场",13,"重庆"));
        list.add(new Zoo("郑州佩奇猪猪",1,"河南"));
        list.add(new Zoo("佩奇猪猪乐园",2,"周口"));
        list.add(new Zoo("野猪王佩奇",3,"河南"));
        list.add(new Zoo("黑猪王佩奇",5,"濮阳"));
        System.out.println("========Stream流的操作====******************");
        System.out.println("**************===Stream中间操作===*****************");
        /**
         * filter筛选操作
         */
        System.out.println("!!!!!!!!!!!!!!!!!=filter=!!!!!!!!!!!!!!!!");
        list.stream().filter(item->item.getAge()>10).forEach(System.out::println);
        /**
         * map映射操作
         */
        System.out.println("------------map映射操作------------");
        list.stream().map(item -> item.getName()).forEach(System.out::println);
        /**
         * sort排序操作
         */
        System.out.println("=================sort的操作=============");
        list.stream().sorted((o1, o2) -> o1.getAge() - o2.getAge()).forEach(System.out::println);
        String arr[]={"1","3","6","8","9","14"};
        String arr1[] = {"14","9","8","6","3","1"};
        Stream<String> arr2 = Stream.of(arr);
        Stream<String> arr3 = Stream.of(arr1);
        /**
         * concat合并
         */
        System.out.println("======cancat合并与操作=======");
        /**
         * distinct去重
         */
        System.out.println("==============distinct==============");
        /**
         * limit 只要截取数据个数
         */
        System.out.println("=========limit=========");

        /**
         * skip去除数据中几个数据
         */
        System.out.println("=======Skip=========!!!");
        Stream.concat(arr2,arr3).skip(4).forEach(System.out::println);
        System.out.println("==========find操作========");
        /**
         * findFirst获取序列中第一条数据
         */
        System.out.println("========findFirst========");
        Optional<Zoo> frist = list.stream().findFirst();
        System.out.println(frist);
        /**
         * findAny随机获取序列中的一条
         */
        System.out.println("==========findAny===========");
        Optional<Zoo> any = list.stream().findAny();
        System.out.println(any);
        System.out.println("===============match==========");
        /**
         * allMatch所以条件均需满足才能实现
         */
        System.out.println("==========allMatch=========");
        boolean b1 = list.stream().anyMatch(item -> item.getAge()>5);
        System.out.println(b1);
        System.out.println("=========count聚合操作========");
        /*max最大值*/
        System.out.println("=====max======");
        Optional<Zoo> max = list.stream().max((o1,o2) -> o1.getAge()-o2.getAge());
        System.out.println(max);
        /*min最小值*/
        System.out.println("============min============");
        System.out.println("============================max最大值==========================");
        Optional<Zoo> min = list.stream().min((o1, o2) -> o1.getAge() - o2.getAge());
        System.out.println(min);
        /*count总数量*/
        System.out.println("============================max最大值==========================");
        long count = list.stream().count();
        System.out.println(count);
        Integer [] arr0={2,3,4,5,6,2};
        System.out.println("============================reduce规约操作可以进行加减乘除还有最值==========================");
        Optional<Integer> reduce = Arrays.stream(arr0).reduce((o1, o2) -> o1 - o2);
        Optional<Integer> reduce1 = Arrays.stream(arr0).reduce((o1, o2) -> o1 + o2);
        Optional<Integer> reduce2 = Arrays.stream(arr0).reduce((o1, o2) -> o1 * o2);
        Optional<Integer> reduce3 = Arrays.stream(arr0).reduce((o1, o2) -> o1 / o2);
        System.out.println(reduce);
        System.out.println("============================collect搜集操作==========================");
        /*获取新的序列集合*/
        List<Zoo> collect = list.stream().collect(Collectors.toList());
        /*获取新的map集合*/
        Map<String, Zoo> collect1 = list.stream().collect(Collectors.toMap(item -> item.getName(), item -> item));
        /*分组*/
        Map<String, List<Zoo>> collect2 = list.stream().collect(Collectors.groupingBy(item -> item.getName()));
        System.out.println(collect2);
        /*最大值*/
        Optional<Zoo> collect3 = list.stream().collect(Collectors.maxBy((o1, o2) -> o1.getAge() - o2.getAge()));
        System.out.println(collect3);
    }
    }

class Zoo{
    private String name;
    private int age;
    private String address;

    public Zoo() {
    }

    public Zoo(String name, int age, String address) {
        this.name = name;
        this.age = age;
        this.address = address;
    }

    @Override
    public String toString() {
        return "Zoo{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", address='" + address + '\'' +
                '}';
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

}